package com.async.handler.impl;

import com.async.domain.entity.AsyncReq;
import com.async.enums.AsyncTypeEnum;
import com.async.enums.ExecStatusEnum;
import com.async.handler.context.AsyncStrategyContext;
import com.async.strategy.DefaultStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 先异步消息处理，失败再保存到数据库
 * </p>
 * @author yang
 * @date 2024/5/17
 */
@Slf4j
@Component
@DefaultStrategy
public class AsyncSaveHandlerService extends AbstractAsyncHandlerService{
    @Override
    public boolean execute(AsyncStrategyContext context) {
        boolean success = asyncProducer.send(context.getAsyncExecDto());
        if (success){
            return true;
        }
        AsyncReq asyncReq = this.saveAsyncReq(context.getAsyncExecDto(), ExecStatusEnum.ERROR.getStatus());

        return Objects.nonNull(asyncReq);
    }

    @Override
    public List<String> listType() {
        return Collections.singletonList(AsyncTypeEnum.ASYNC_SAVE.name());
    }
}
